//
//  LBButton.m
//  BusinessCloud
//
//  Created by Apple on 2017/7/20.
//  Copyright © 2017年 yxk. All rights reserved.
//

#import "LBButton.h"

@interface LBButton ()

@property (nonatomic,assign) CGFloat marginIg;//间隙
@property (nonatomic,assign) YFUIButtonType Coutomtype;
@property (nonatomic,strong) NSDictionary *colorDic;

@end
@implementation LBButton
-(NSDictionary *)colorDic{
    if (!_colorDic) {
        _colorDic = [NSDictionary dictionary];
    }
    return _colorDic;
}

+(instancetype)buttonWithCoutomType:(YFUIButtonType)type{
    return [[self alloc]initWithCoutomType:type];;
}
- (instancetype)initWithCoutomType:(YFUIButtonType)type{
    if (self = [super init]) {
        _Coutomtype = type;
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.marginIg = 4.0;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    switch (_Coutomtype) {
        case YFUIButtonTypeImageUp:
            [self alignmentTop];
            break;
        case YFUIButtonTypeImageRight:
            [self alignmentRight];
            break;
        case YFUIButtonTypeImageDown:
            [self alignmentBottom];
            break;
            
        default:
            break;
    }
}
#pragma mark - 重新定义间隙
-(void)setMargin:(CGFloat)margin{
    _margin = margin;
    self.marginIg = margin;
}

#pragma mark - 图片在右侧
- (void)alignmentRight{
    // 图片右移
    CGSize imageSize = self.imageView.frame.size;
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width * 2 - self.marginIg, 0.0, 0.0);
    
    // 文字左移
    CGSize titleSize = self.titleLabel.frame.size;
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, - titleSize.width * 2 - self.marginIg);
}

#pragma mark - 图片在上侧
- (void)alignmentTop{
    // 计算文本的的宽度
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[NSFontAttributeName] = self.titleLabel.font;
    CGRect frame = [self.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictM context:nil];
    
    CGFloat btnW = self.frame.size.width;
    CGFloat btnH = self.frame.size.height;
    
    CGFloat titleLabelH = frame.size.height;
    CGFloat imageViewH = btnH - titleLabelH - self.marginIg * 3-5;
    CGFloat margin = (btnW - imageViewH) / 2;
    
    self.imageView.frame = CGRectMake(margin, self.marginIg, imageViewH, imageViewH);
    self.titleLabel.frame = CGRectMake(0 , imageViewH + self.marginIg * 2,btnW, btnH - imageViewH - self.marginIg * 3);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}

#pragma mark - 图片在下侧
- (void)alignmentBottom{
    // 计算文本的的宽度
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[NSFontAttributeName] = self.titleLabel.font;
    CGRect frame = [self.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictM context:nil];
    
    CGFloat btnW = self.frame.size.width;
    CGFloat btnH = self.frame.size.height;
    
    CGFloat titleLabelH = frame.size.height;
    CGFloat imageViewH = btnH - titleLabelH - self.margin * 3;
    CGFloat margin = (btnW - imageViewH) / 2;
    
    self.titleLabel.frame = CGRectMake(0 , self.margin, btnW, btnH - imageViewH - self.margin * 3);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.imageView.frame = CGRectMake(margin, self.titleLabel.frame.size.height + self.margin * 2, imageViewH, imageViewH);
}

#pragma mark - 根据状态改变背景颜色
- (void)setBackgroudColor:(UIColor *)color forState:(UIControlState)state{
    [self setBackgroundImage:[self imageWithColor:color] forState:state];
}
//  颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end
